﻿function CellEditHandler() {
	var _this = this;
	this.grid = null;
	
	this.editCell = function(e) {
		if (e.type == 'keydown' && e.keyCode < 48) return;
		var coldef = _this.grid.getColumnDefinition(_this.grid.getHeaderCells().get(this.cellIndex));
		if (coldef.editor != null) {
			var value = $(this).text();
			$(this).empty().addClass('editing');
			var _this = this;
			coldef.editor.attach(this, value, function(newValue, cancelled) {
				_this.grid.setCellValue(_this, cancelled != true ? newValue: value);
				$(_this).removeClass('editing');
				$(_this).focus();
				$(_this.grid).resize();
			});
		};
	};
	
	//if (e.keyCode >= 48) $(this).dblclick();	
	this.bind = function(unbind) {
		if (!unbind) {
			this.grid.getDataCells().bind('dblclick keydown', _this.editCell);
		} else {
			this.grid.getDataCells().unbind('dblclick keydown', _this.editCell);
		}
	};
	
	this.attach = function(grid) {
		this.grid = grid;
		this.grid.pageContentChanged.add(function() {
			_this.bind();
		});
		
		this.bind();
	};
	
	this.detach = function() {
		this.bind(true);
	};
}; CellEditHandler.inherits(DatagridPlugin);